package cn.com.lsjz.nursemanager.controller.system;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.com.lsjz.commons.result.ResultModel;
import cn.com.lsjz.commons.utils.GraphicHelper;
import cn.com.lsjz.nursemanager.model.system.SysDept;
import cn.com.lsjz.nursemanager.model.system.SysOperation;
import cn.com.lsjz.nursemanager.model.system.SysUser;
import cn.com.lsjz.nursemanager.model.vo.HisPatientMasterInfoVo;
import cn.com.lsjz.nursemanager.service.system.HisPatMasterInforService;
import cn.com.lsjz.nursemanager.service.system.SysDeptService;
import cn.com.lsjz.nursemanager.service.system.SysOperationService;
import cn.com.lsjz.nursemanager.service.system.SysRoleService;
import cn.com.lsjz.nursemanager.service.system.SysUserService;


@RestController
public class LoginController {
	
	@Autowired
	private SysUserService userService;
	
	@Autowired
	private SysDeptService deptDictService;
	
	@Autowired
	private SysRoleService roleService;
	
	@Autowired
	private SysOperationService moduleDictService;
	
	@Autowired
	private HisPatMasterInforService patInforService;
	
	// 1、 用户登录校验
	@RequestMapping(value="doLogin",method=RequestMethod.POST)
	public ResultModel login(String userId,String password) {
		ResultModel result = new ResultModel();
		SysUser user = userService.getUser(userId, password); 
		if(user == null) {
			result.setSuccess(false);
			result.setMessage("用户名或密码错误！");
		}else {
			result.setSuccess(true);
			result.setMessage("登录成功！");
			result.setData(user);
		}
		return result;
	}
	
	// 2、 获取当前的用户的所有权限信息
	@RequestMapping(value="loadUserAuthority",method=RequestMethod.POST)
	public ResultModel loadUserAuthority(String userId) {
		ResultModel result = new ResultModel();
		// 获取科室权限信息
		List<SysDept> deptDicts = deptDictService.getDeptDictByUserId(userId);
		// 获取用户角色信息
		String roleStr = roleService.getRolesByUserId(userId);
		// 获取用户菜单信息
		List<SysOperation> moduleDicts = moduleDictService.loadModuleByUserId(userId);
		
		Map<String, Object> dataMap = new HashMap<>();
		dataMap.put("deptDicts", deptDicts);
		dataMap.put("roleStr", roleStr);
		dataMap.put("moduleDicts", moduleDicts);
		result.setData(dataMap);
		result.setSuccess(true);
		result.setMessage("查询成功");
		return result;
	}
	
	// 3、 获取当前科室的在院患者信息
	@RequestMapping(value="loadPatsList",method=RequestMethod.POST)
	public ResultModel loadPatsList(String deptCode) {
		List<HisPatientMasterInfoVo> patientInfoVos = patInforService.getPatientList(deptCode);
		return new ResultModel(true, patientInfoVos, "查询成功");
	}
	
	/**
	 * 生成验证码
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value="verify/regist")
	public void verifyRegist(HttpSession session,HttpServletResponse response) throws IOException {
		final int width = 120; // 图片宽度  
	    final int height = 35; // 图片高度  
	    final String imgType = "jpeg"; // 指定图片格式 (不是指MIME类型)  
	    final OutputStream output = response.getOutputStream(); // 获得可以向客户端返回图片的输出流  
	    String code = GraphicHelper.create(width, height, imgType, output);  
	    session.setAttribute("verify/regist", code);  
	}
	
	/**
	 * 验证码校验
	 * @param session
	 * @param code
	 * @return
	 */
	@RequestMapping(value="verify/check",method=RequestMethod.POST)
	public ResultModel verifyCheck(HttpSession session,String code){
		String codeBefore = (String) session.getAttribute("verify/regist");
		session.removeAttribute("verify/regist"); // 移除验证码
		if(codeBefore.toUpperCase().equals(code.toUpperCase())) {
			return new ResultModel(true, "验证成功");
		}else {
			return new ResultModel(false, "验证码不匹配");
		}
	}
	
}
